package org.example.server.controller;

import lombok.RequiredArgsConstructor;
import org.example.common.properties.JwtProperties;
import org.example.common.result.Result;
import org.example.common.utils.jwtUtil;
import org.example.pojo.user.userLogin;

import org.example.server.sever.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequiredArgsConstructor
public class login {

    private final JwtProperties jwtProperties;

    private final userService userServiceImpl;

    @RequestMapping(value="/login",method = RequestMethod.POST)
    public Result login(@RequestBody userLogin login) {

        Integer login1 = userServiceImpl.login(login);

            Map<String, Object> map = new HashMap<>();

            map.put("id", login1);
            map.put("username", login.getYouxiang());
            System.out.println("登录成功");

            // 登录成功后，生成token
            String token = jwtUtil.buildToken(map, jwtProperties.getAdminTtl(),jwtProperties.getAdminSecretKey());
            return Result.success(token) ;
    }

}
